/**
 * @class common.echarts
 * echarts数据解析类
 * @singleton
 */
define(function(require) {
    var tooltip_defaults = {
        trigger: 'axis',
        backgroundColor: 'rgba(255,255,255,0.9)',
        borderColor: '#333',
        borderWidth: 2,
        borderRadius: 3,
        padding: 8,
        textStyle: {
            color: '#333'
        }
    };
    return {
        /**
         * 绘制一个曲线图表
         * @param {String} id id
         * @param {Object} params 图表数据和配置参数
         * */
        line: function(id, params) {
            var chart = echarts.init($(id)[0]);
            var option = {
                tooltip: tooltip_defaults,
                toolbox: {
                    show: false
                },
                legend: {
                    data: []
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: [],
                        splitLine: {
                            show: false
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLine: {
                            show: false
                        },
                        splitLine: {
                            lineStyle: {
                                type: 'dashed',
                                color: '#afafaf'
                            }
                        }
                    }
                ],
                series: []
            };
            var series_data = {};
            _.each(params.data, function(val, key) {
                option.xAxis[0].data.push(val.date);
                _.each(val, function(v, k) {
                    if (k == 'date') return;
                    if (_.empty(series_data[k])) {
                        series_data[k] = [v];
                    } else {
                        series_data[k].push(v);
                    }
                });
            });
            _.each(series_data, function(val, key) {
                var name = params.config.series[key].name;
                option.series.push({
                    name: name,
                    type: 'line',
                    smooth: true,
                    itemStyle: {
                        normal: {
                            color: params.config.series[key].color
                        }
                    },
                    data: val
                });
                option.legend.data.push(name);
            });
            chart.setOption(option);
        },
        /**
         * 绘制一个圆饼图表
         * @param {String} id id
         * @param {Object} params 图表数据和配置参数
         * */
        pie: function(id, params) {
            var chart = echarts.init($(id)[0]);
            var option = {
                tooltip: _.defaults({
                    trigger: 'item',
                    formatter: "{b} : {c} ({d}%)"
                }, tooltip_defaults),
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    data: []
                },
                series: [
                    {
                        type: 'pie',
                        radius: [40, 90],
                        itemStyle: {
                            normal: {
                                labelLine: {
                                    show: false
                                }
                            }
                        },
                        data: []
                    }
                ]
            };
            _.each(params.data, function(val, key) {
                var conf_key = null,
                    label = "";
                _.each(val, function(v, k) {
                    if (k != 'count') {
                        conf_key = k + '_' + v;
                        label = v;
                    }
                });
                var name = "",
                    color = null;
                try {
                    name = params.config.series[conf_key].name;
                    color = params.config.series[conf_key].color;
                } catch (e) {
                    name = label;
                    color = null;
                }
                option.series[0].data.push({
                    name: name,
                    itemStyle: {
                        normal: {
                            color: color,
                            label: {
                                show: true,
                                position: 'inner',
                                formatter: "{d}%"
                            }
                        },
                        emphasis: {
                            label: {
                                show: true,
                                position: 'inner',
                                formatter: "{d}%"
                            }
                        }
                    },
                    value: val.count
                });

                option.legend.data.push(name);
            });

            chart.setOption(option);
        },
        /**
         * 绘制一个横向柱形图表
         * @param {String} id id
         * @param {Object} params 图表数据和配置参数
         * */
        bar_x: function(id, params) {
            var chart = echarts.init($(id)[0]);
            var option = {
                tooltip: tooltip_defaults,
                legend: {
                    data: []
                },
                xAxis: [
                    {
                        type: 'value'
                    }
                ],
                yAxis: [
                    {
                        type: 'category',
                        data: []
                    }
                ],
                series: []
            };
            _.each(params.data, function(val, key) {
                var conf_key = null;
                _.each(val, function(v, k) {
                    if (k != 'count')
                        conf_key = k + '_' + v;
                });
                var name = params.config.series[conf_key].name;
                option.series.push({
                    name: name,
                    type: 'bar',
                    stack: 'count',
                    itemStyle: {
                        normal: {
                            label: {
                                show: true,
                                position: 'inside'
                            }
                        }
                    },
                    data: [val.count]
                });
                option.legend.data.push(name);
            });
            option.yAxis[0].data.push(params.config.yAxis);
            chart.setOption(option);
        },
        /**
         * 绘制一个中国省份图表
         * @param {String} id id
         * @param {Object} params 图表数据和配置参数
         * */
        map_province: function(id, params) {
            var chart = echarts.init($(id)[0]);
            var regions = "北京,天津,上海,重庆,河北,河南,云南,辽宁,黑龙江,\
                    湖南,安徽,山东,新疆,江苏,浙江,江西,湖北,广西,甘肃,山西,\
                    内蒙古,陕西,吉林,福建,贵州,广东,青海,西藏,四川,宁夏,海南,\
                    台湾,香港,澳门";
            var seriesData = [];
            _.each(_.explode(',', regions), function(val) {
                val = _.trim(val);
                seriesData.push({
                    name: val,
                    value: 0
                });
            });
            var option = {
                tooltip: _.defaults({
                    trigger: 'item'
                }, tooltip_defaults),
                dataRange: {
                    min: 0,
                    max: 0,
                    text: ['高', '低'],
                    y: 'top'
                },
                series: [
                    {
                        name: params.config.series.name,
                        type: 'map',
                        mapType: 'china',
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true
                                }
                            },
                            emphasis: {
                                label: {
                                    show: true
                                }
                            }
                        },
                        data: seriesData
                    }
                ]
            };
            _.each(params.data, function(val) {
                if (val.count > option.dataRange.max) {
                    option.dataRange.max = val.count;
                }
                _.update('$.series[0].data[?(@.name=="' + val.province + '")].value', val.count, option);
            });
            chart.setOption(option);
        }
    };
});